package com.runen.infinitemovement.activity;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;


import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.ImageScaleAdapter;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.widget.HackyViewPager;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import uk.co.senab.photoview.PhotoView;

public class PhotoActivity extends Activity {
    PhotoView photoview;
    @BindView(R.id.viewPager)
    HackyViewPager viewPager;
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.page)
    TextView page;
    private String photo;
    private String tag;
    private ArrayList<String> urlList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);
        ButterKnife.bind(this);
        //initData();
        initView();
        initData2();
    }

    private void initView() {

    }

    private void initData2() {
        urlList = getIntent().getStringArrayListExtra("list");

        int position = getIntent().getIntExtra("position", 0);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                page.setText(position + 1 + "/" + urlList.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        ImageScaleAdapter adapter = new ImageScaleAdapter(this, urlList);
        viewPager.setAdapter(adapter);
        //设置默认选中的页
        viewPager.setCurrentItem(position);
        page.setText(position + 1 + "/" + urlList.size());
        viewPager.setSelected(true);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        photo = getIntent().getStringExtra("photo");
        tag = getIntent().getStringExtra("tag");
        if (photo.contains("http://") || !TextUtils.isEmpty(tag)) {
            final Drawable[] drawable = {null};
            try {
                CommonUtil.runInThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            drawable[0] = Drawable.createFromStream(
                                    new URL(photo).openStream(), "");
                            final Drawable finalDrawable = drawable[0];
                            CommonUtil.runOnUIThread(new Runnable() {
                                @Override
                                public void run() {
                                    photoview.setImageDrawable(finalDrawable);
                                }
                            });
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            } catch (Exception e) {
                Log.d("test", e.getMessage());
            }
        } else {
            photoview.setImageDrawable(Drawable.createFromPath(photo));
        }
    }
}